package edu.upol.paradise.compiler.types;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import edu.upol.paradise.utils.Utils;

public class VariablePassingWrapper {
	static final Logger log = Logger.getLogger(VariablePassingWrapper.class);
	
	private static class Parameter {
		private ParadiseObject[] objects;
		boolean isAggregate;
	}
	
	private HashMap<String, Parameter> args = new HashMap<String, Parameter>();

	public ParadiseObject[] asBaseObjectArray() throws Exception {
		ParadiseObject[] aargs = new ParadiseObject[0];
		List<ParadiseObject> argList = new ArrayList<ParadiseObject>();
		
		for (Parameter p : args.values()){
			for (ParadiseObject o : p.objects)
				argList.add(o);
		}
		
		return argList.toArray(aargs);
	}
	
	public void addArgument(String bindName, ParadiseObject o){
		addArgument(bindName, new ParadiseObject[]{o}, false);
	}
	
	public void addArgument(String bindName, ParadiseObject[] value, boolean isAggregate){
		Parameter p = new Parameter();
		p.objects = value;
		p.isAggregate = isAggregate;
		args.put(bindName, p);
	}

	public int getArgCount(){
		return args.size();
	}
	
	public Set<String> getArgNames(){
		return args.keySet();
	}
	
	public ParadiseObject getSingleObject(String varg){
		Parameter p = args.get(varg);
		if (p.isAggregate)
			return Utils.array2list(p.objects);
		else
			return p.objects[0];
	}
	
	public ParadiseObject[] getRaw(String varg){
		return args.get(varg).objects;
	}
	
}
